<template>
  <div>
    <vxe_table
      :options="options"
      @handleTableBtn="handleTableBtn"
      ref="baseTable"
    >
      <template #btn>
        <el-button type="success" icon="el-icon-plus" @click="add"
        v-permission="['btn_iot_add']"
          >添加OTA文件</el-button
        >
        <!-- v-permission="['btn_sysUserAdmin_add']" -->
      </template>
    </vxe_table>
    <addDialog ref="add" @success="success"></addDialog>
  </div>
</template>

<script>
import vxe_table from "@/components/table/vxe_table.vue";
import tableMixin from "@/mixins";
import addDialog from "./comps/addDialog.vue";
import { list, set } from "@/api/user";
import { hasBtnAuth, parseTime } from "@/utils";
import { delOtaFile, downUpOtaFile, getOtaList } from "@/api/device";
export default {
  mixins: [tableMixin],
  components: {
    vxe_table,
    addDialog,
  },
  data() {
    return {
      options: {
        id: "usertable",
        apiFn: getOtaList,
        columns: [
          {
            field: "title",
            title: "文件标题",
            search: true,
            valueType: "input",
          },
          {
            field: "bucket",
            title: "Bucket",
          },
          {
            field: "fileName",
            title: "文件名",
          },
          {
            field: "type",
            title: "类型",
            type: "html",
            formatter: ({ row }) => {
              if(row.type==9){
                return '合宇'
              }else{
                let arr = ['显示板','XY轴板','驱动板A','驱动板B','驱动板C','驱动板D']
                return arr[row.type]
              }
            },
          },
          {
            field: "state",
            title: "类型",
            type: "html",
            formatter: ({ row }) => {
              if (row.state == 0) {
                return "未上架";
              } else {
                return "已上架";
              }
            },
          },
          {
            field: "ver",
            title: "版本",
          },
          {
            field: "binHash",
            title: "哈希",
          },
          {
            field: "size",
            title: "大小",
          },
          {
            field: "updateTime",
            title: "时间",
            type: "html",
            formatter: ({ row }) => parseTime(row.updateTime),
          },
        ],
        btnWidth: 250,
        btn: [
          {
            title: "编辑",
            handleName: "edit",
            type: "success",
            show: (row) => row.state == 0,
            handleName: "handleEdit",
          },
          {
            title: "上架",
            handleName: "handleUp",
            show: (row) => row.state == 0 && hasBtnAuth("btn_iot_updown"),

          },
          {
            title: "下架",
            handleName: "handleDown",
            type: "danger",
            show: (row) => row.state == 1   && hasBtnAuth("btn_iot_updown"),
          },
          {
            title: "删除",
            handleName: "handleDel",
            type: "danger",
            show: (row) => row.state == 0  && hasBtnAuth("btn_iot_del"),
          },
        ],
      },
    };
  },

  methods: {
    edit(row, index) {
      console.log(row, index);
    },
    handleEdit(row){
      this.$refs.add.open(row);
    },
    handleUp(row) {
      this.$confirm("确定上架", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          
          const res = await downUpOtaFile({
            id: row.id,
            state: 1,
          });
          if (res.status == 0) {
            this.$message({
              type: "success",
              message: "success",
            });
            this.success();
          }
        })
        .catch(() => {});
    },
    handleDown(row) {
      this.$confirm("确定下架", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await downUpOtaFile({
            id: row.id,
            state: 0,
          });
          if (res.status == 0) {
            this.$message({
              type: "success",
              message: "success",
            });
            this.success();
          }
        })
        .catch(() => {});
    },
    handleDel(row){
      this.$del({id:row.id},delOtaFile)
    }
  },
};
</script>
